<?php

	class RClassControllerAdvert
	{
		var $table;
		
		var $action = 'add';
		
		var $params;
		
		var $options;
		
		var $columns;
		
		var $values;
		
		function __construct($options, $params)
		{
			$this->params = $params;
			$this->options = $options;
			$this->action = $options['action'];
			$this->table = $options['module'].'_'.$options['form'].'_'.$options['type'];
			
		}
		
		function process()
		{
			$this->{$this->action}();
		}
		
		private function add()
		{
			if (empty($_POST))
			{
				$template = RTemplate::getTemplate();
				$handler = asmpth(BPM, 'views', $this->options['type'], $this->options['action'], $this->options['form'].'.tpl');
				$template->registerVar('content', file_get_contents($handler));
			}
			else $this->save();
		}
		
		private function save()
		{
			
			$post = & $_POST;
			$db = RClassDb::getInstance();
			
			$reqired = $this->params['required'];			
			unset($this->params['required']);
			
			$tables = $this->params['help_tables'];
			unset($this->params['help_tables']);
			$tables[$this->table] = $this->params;
			
			foreach ($tables as $table => $params)
			{
				$this->columns[$table] = '(';
				$this->values[$table] = '(';
				
				foreach ($params as $name => $type)
				{
					if (isset($post[$name]) && !is_array($type))
					{
						$this->columns[$table] .= $name.', ';
						if (!is_array($post[$name]))
							$this->appendValue($post[$name], $type, $table);
						else $this->appendValue(NULL, $params, $table);
					}
					elseif (!isset($post[$name]))
					{
						//echo $name, '<br />';
					}
				}
				
				$this->values[$table] = trim($this->values[$table], ', ').')';
				$this->columns[$table] = trim($this->columns[$table], ', ').')';
			}
			
			foreach ($this->columns as $table => $columns)
			{
				$query = 'INSERT INTO '.$table.' '.$columns.' VALUES '.$this->values[$table];
				$db->setValues($query);
			}
		}
		
		private function appendValue($value, $type, $table)
		{
			if (!is_array($type) && $value !== NULL)
			{
				$ts = explode('-', $type);
				if (setexist($ts[1]))
					CheckDataSize($ts[1], $value);
				$this->values[$table] .= $this->wrapUp($value, $ts[0]).', ';
			}
			else
			{	
				foreach ($type as $n => $t)
				{
					$ts = explode('-', $t);
					if (!is_array($post[$n]))
					{
						if (setexist($ts[1]))
							CheckDataSize($ts[1], $post[$n]);
						$this->values[$table] .= $this->wrapUp($post[$n], $ts[0]).', ';
					}
					else
					{
						$rowcount = count($post[$n]);
						$keys = array_keys($type);
						$tmp = array();
						for ($i = 0; $i < $rowcount; $i++)
						{
							foreach ($keys as $key)
								$tmp2 = $this->wrapUp($post[$key][$i], $ts[0]).', ';
							$tmp[] = $tmp2;
						}
						$this->values[$table] .= explode('), (', $tmp);
						unset($tmp);
					}
				}
			}
		}
		
		private function wrapUp($value, $type)
		{
			switch ($type)
			{
				case 'int':
					return (int)$value;
					break;
					
				case 'date':
					$date = explode('.', $value);
					foreach ($date as $i => $d)
						$date[$i] = (int)$d;
					return implode('-', array_reverse($date));
					break;
					
				case 'datetime':
					$datetime = explode(' ', $value);
					$date = explode('.', $datetime[0]);
					foreach ($date as $i => $d)
						$date[$i] = (int)$d;
					return implode('-', array_reverse($date)).' '.$datetime[1];
					break;
					
				case 'str':
				default:
					return '"'.mysql_real_escape_string($value).'"';
					break;
			}
		}
	}